

import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'models/collect_configuration.dart';
import 'models/payment_intent.dart';
import 'stripe_connection_plugin_platform_interface.dart';

/// An implementation of [StripeConnectionPluginPlatform] that uses method channels.
class MethodChannelStripeConnectionPlugin extends StripeConnectionPluginPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('stripe_connection_plugin');


  @override
  Future<void> permissionAllowed() async{
    await methodChannel.invokeMethod<void>('permissionAllowed');
  }

  @override
  Future<void> init(String sn,String localId,String baseUrl,String errorUrl) async{
    await methodChannel.invokeMethod<bool>('init',{
    "sn":sn,
    "localId":localId,
    "baseUrl":baseUrl,
    "errorUrl":errorUrl
    });
  }

  @override
  Future<void> cashBox(String cashBoxNum) async{
    await methodChannel.invokeMethod<void>('cashBox',{
      "cashBoxNum":cashBoxNum,
    });
  }
  @override
  Future<bool> openCash() async{
    return await methodChannel.invokeMethod("openCash");
  }


  @override
  Future<StripePaymentIntent> retrievePaymentIntent(
      // Client secret of the payment intent which you want to collect payment mwthod for
      String clientSecret, {

        /// Configruation for the collection process
        CollectConfiguration? collectConfiguration = const CollectConfiguration(
          skipTipping: true,
        ),
      }) async {
    Map paymentIntent = await methodChannel.invokeMethod("retrievePaymentIntent", {
      "paymentIntentClientSecret": clientSecret,
      "collectConfiguration": collectConfiguration?.toMap(),
    });

    return StripePaymentIntent.fromMap(paymentIntent);
  }


  @override
  Future<void> disconnectReader() async{
     await methodChannel.invokeMethod<void>("disconnectReader");
  }

  ///取消等待刷卡状态
  @override
  Future<bool> cancelRetrievePaymentIntent() async {
    bool cancel = await methodChannel.invokeMethod("cancelRetrievePaymentIntent");
    return cancel;
  }

  /// 进行支付
  @override
  Future<Map> processPayment() async {
    Map map = await methodChannel.invokeMethod("processPayment");
    return map;
  }

  ///取消支付
  @override
  Future<Map> cancelProcessPayment() async {
    Map map = await methodChannel.invokeMethod("cancelProcessPayment");
    return map;
  }

  @override
  Future<String> androidRequest() async{
    String string= await methodChannel.invokeMethod("androidRequest");
    return string;
  }
}
